/**
 * Created by Administrator on 2018/10/17 0017.
 */

$(function () {
    $('#search').click(function () {
        var id = $('#role_id').val();
        var keyword =$('#keyword').val();
        getMemListPage(url_list.member_list,id,keyword);
        return false;
    });
    $('#role_id').change(function () {
        var id = $('#role_id').val();
        var keyword =$('#keyword').val();
        getMemListPage(url_list.member_list,id,keyword);
        return false;
    })
})

var ids=[];

function getMemListPage(pageurl,id,keyword,curr){
    $("#allChk").prop("checked",false);
    ids.push(id);
    sign.params={
        identity:id,
        keyword:keyword,
        curr:curr
    };
    var encode_post = sign.encode_post();
    layui.use(['laypage','form', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer
            ,form = layui.form;
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});

        $.post(pageurl,encode_post, function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }

            datass = sign.decode_post(data);

            //选择
            member_list(datass);

            form.render('checkbox');
            layer.close(index);
            if(datass.data.pages<=1){
                $('#pages').html('');
            }
            else {
                //显示分页
                laypage.render({
                    elem: 'pages', 			// 容器
                    count: datass.data.total,     	// 总页数(后台的)
                    curr: datass.data.curr, 		//当前页(后台获取到的)
                    prev: '&laquo;',
                    next: '&raquo;',
                    jump: function (obj, first) {
                        if (!first) {
                            getListPage(pageurl, id,keyword,1, obj.curr);
                        }
                    }
                });
            }
        });
    });
}

function member_list(data) {
    var html='';
    if (data['data']['list'] === undefined || data['data']['list'].length == 0) {
        html ='<tr><td align="center" colspan="12">暂无相关数据</td></tr>';
        //页面数据替换
        $('#page_list').html(html);
    }
    else {
        list = data['data']['list'];
        for (var i in list) {
            list[i].id = list[i].id ? list[i].id : '';
            list[i].username = list[i].username ? list[i].username : '';
            list[i].realname = list[i].realname ? list[i].realname : '';
            list[i].email = list[i].email ? list[i].email : '';
            list[i].phone = list[i].phone ? list[i].phone : '';
            list[i].introduce = list[i].introduce ? list[i].introduce : '';
            list[i].img = list[i].img ? list[i].img : '';
            list[i].role_name = list[i].role_name ? list[i].role_name  : '超级管理员';
            list[i].createtime = list[i].createtime ? list[i].createtime : '';

            html += '<tr> <td class="tc"><input name="id[]" value="' + list[i].id + '" type="checkbox"></td> <td>';
            html += list[i].username + '</td>';
            html += '<td>' + list[i].realname + '</td>';
            html += '<td>' + list[i].email + '</td>';
            html += '<td>' + list[i].phone + '</td>';
            html += '<td>' + list[i].introduce + '</td>';
            html += '<td><img style="width: 40px;height: 30px;;" src="/'+list[i].img+'"></td>';
            html += '<td>' + list[i].role_name + '</td>';
            html += '<td>' + list[i].createtime + '</td>';


            html += '<td>'+list[i].updatetime+'</td><td class="link-update">';
            html += '<a class="link-update" href="#" onclick="oncli('+list[i].id+')">';
            html += '改角</a>';
            html += '<a class="link-del" href="#" onclick="del_member(' + list[i].id + ')">删除</a>';
            html += '</td></tr>';
        }
        $('#page_list').html(html);
    }
}

//修改弹出层角色信息
function oncli(id) {
    document.getElementById('member_id').value=id;
    $('.bgPop,.pop').show();
}
function outcli() {
    $('.bgPop,.pop').hide();
}

function update_user() {
    var member_id = $('#member_id').val();
    var role_id = $('#role_ids').val();
    sign.params={
        id:member_id,
        identity:role_id
    };
    var encode_post = sign.encode_post();
    $.post(url_list.user_role_update,encode_post, function(data){
        //解码
        if(typeof data == 'string'){
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        if (data.code==200){
            layer.msg(data.msg, {icon: 1, time: 1500});
            getMemListPage(url_list.member_list);
            outcli();
        }else {
            layer.msg(data.msg, {icon: 2, time: 1500});
        }
    });
}

//删除
function del_member(id) {
    var id = id;
    sign.params={
        id:id,
    };
    var encode_post = sign.encode_post();
    $.post(url_list.member_del,encode_post, function(data){
        getMemListPage(url_list.member_list);
    });
}
//批量删除
function dels_member() {
    var checkedNum = $("input[name='id[]']:checked").length;
    if (checkedNum == 0) {
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    if (confirm("确定要删除？")) {
        var checkedList = new Array();

        $("input[name='id[]']:checked").each(function () {
            checkedList.push($(this).val());
        });
    }
    var check_id =checkedList.join(',');
    sign.params={
        id :check_id,
    };
    var encode_post = sign.encode_post();
    $.post(url_list.member_del,encode_post,function (data) {
        getMemListPage(url_list.member_list);
    })

}

//全选
$("#allChk").click(function() {
    $("input[name='id[]']").prop("checked",this.checked);
});

//批量导出
function user_export() {
    var id = $('#role_id').val();
    var keyword =$('#keyword').val();
    sign.params={
        identity:id,
        keyword:keyword
    };
    var encode_post = sign.export_post();
    encode_post=encode_post.replace(/"/g,'');
    location.href=url_list.member_export+'/'+encode_post;
}

